-- DragonKing
-- Created by chengb Aug/5/2016
-- 龙王格子

-- 龙王对应的冈布奥的编号
local DRAGON_KING_PET_ID = 83;

-- 初始化
function initDragonKing(self)
    self.item:removeAllChildren();

    local buildingInfo = BuildingBonusM.queryBuildingView(self.gridData.class);
    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local iconPath = getHeroIconPath(DRAGON_KING_PET_ID);

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 接受
            local function accept()
                DungeonActionM.go("join_dragon_king", self.gridData:getPos(), BraveVSDragonM.CHOOSE_TYPE_ACCEPT);
            end

            -- 再次拒绝
            local function refuseAgain()
                if DungeonActionM.go("join_dragon_king", self.gridData:getPos(),BraveVSDragonM.CHOOSE_TYPE_REFUSE_AGAIN) then
                    local tip = getLocStr("refuse_dragon_king_tip");
                    local alertList = splitString(tip, "\n");
                    splitAlert(alertList);
                    playBuildingDisappear(self);
                end
            end

            -- 拒绝
            local function refuse()
                DungeonActionM.go("join_dragon_king", self.gridData:getPos(), BraveVSDragonM.CHOOSE_TYPE_REFUSE);

                -- 拒绝后直接弹出第二个框
                local function delayFunction()
                    self:openBuildingView2(accept, refuseAgain, getLocStr("btn_text_accept"),
                        getLocStr("btn_text_refuse"), buildingInfo["dialog2"], iconPath);
                end
                performWithDelay(self, delayFunction, 0.3);
            end

            local choose = BraveVSDragonM.getDragonKingJoinChoose();
            if choose == nil then
                -- 还没做过选择
                self:openBuildingView2(accept, refuse, nil, nil, nil, iconPath);
            elseif choose == BraveVSDragonM.CHOOSE_TYPE_REFUSE then
                -- 拒绝了龙王的入伙请求
                self:openBuildingView2(accept, refuseAgain, getLocStr("btn_text_accept"),
                    getLocStr("btn_text_refuse"), buildingInfo["dialog2"], iconPath);
            end
        end
    end

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.icon:setScale(1.2);
    self.item:setPositionY(-10);
    
    -- 呼吸
    self.needAnimation = true;

    -- 如果接受或再次拒绝过龙王，格子要隐藏
    local choose = BraveVSDragonM.getDragonKingJoinChoose();
    if choose == BraveVSDragonM.CHOOSE_TYPE_ACCEPT or choose == BraveVSDragonM.CHOOSE_TYPE_REFUSE_AGAIN then
        self.icon:setVisible(false);
    else
        self.icon:setVisible(true);
    end
end
